package cn.dslcode.security.config;

import cn.dslcode.security.common.DataCenter;
import cn.dslcode.security.config.filter.CustomCaptchaVerifyFilter;
import cn.dslcode.security.config.handler.CustomAccessExceptionHandler;
import cn.dslcode.security.config.handler.CustomLogoutSuccessHandler;
import cn.dslcode.security.config.provider.CustomAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
//用于@PreAuthorize的生效,基于方法的权限控制，允许进入页面方法前检验
@EnableGlobalMethodSecurity(prePostEnabled = true)
//覆盖默认的spring security提供的配置
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationProvider customAuthenticationProvider;
    @Autowired
    private CustomLogoutSuccessHandler customLogoutSuccessHandler;
    @Autowired
    private CustomAccessExceptionHandler customAccessExceptionHandler;
    @Autowired
    private CustomCaptchaVerifyFilter customCaptchaVerifyFilter;

    /**
     * 忽略静态文件
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/js/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().antMatchers("/", "/login/**", "/authentication/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilterBefore(customCaptchaVerifyFilter, UsernamePasswordAuthenticationFilter.class)
            .formLogin()
            .loginPage(DataCenter.loginFormUri)
            .permitAll()
//            .successHandler(authenticationSuccessHandler)
//            .failureHandler(authenticationFailureHandler)
            .and()
            .logout()
            .logoutUrl(DataCenter.logoutUri)
            .clearAuthentication(true)
            //.deleteCookies()
            .invalidateHttpSession(true)
            .logoutSuccessHandler(customLogoutSuccessHandler)
            //.logoutSuccessUrl("/authentication/require?logout")
            .permitAll()
            .and()
            .exceptionHandling()
//            .accessDeniedPage("/error/deny")
            .authenticationEntryPoint(customAccessExceptionHandler)
            .accessDeniedHandler(customAccessExceptionHandler)
            .and()
            .headers().frameOptions().disable()
            .and()
            .csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider);
    }

}